/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.server.impl;

import pt.utl.ist.isr.livetrack.server.interfaces.SequentialFileCamera;
import pt.utl.ist.isr.livetrack.analysis.ImageUtils;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class SequentialFileCameraImpl extends DefaultCamera implements SequentialFileCamera {

    private File dir;
    private String[] images;
    private int currentImage = 0;
    private BufferedImage bi;

    public SequentialFileCameraImpl() {
        super();
    }

    public SequentialFileCameraImpl(String id, String dir) {
        super(id);
        setDir(dir);
    }

    @Override
    public String getDir() {
        return dir == null ? "" : dir.getAbsolutePath();
    }

    @Override
    public synchronized void setDir(String d) {
        dir = new File(d);
        images = dir.list();
        Arrays.sort(images);
        currentImage = 0;
    }

    @Override
    public String getType() {
        return getClass().getSimpleName().replace("Impl", "");
    }

    @Override
    public synchronized void acquireImage() {
        if (currentImage < images.length) {
            try {

                File file = new File(dir.getAbsolutePath() + "/" + images[currentImage++]);
                ImageInputStream iis = ImageIO.createImageInputStream(file);

                String[] split = file.getName().split("\\.");
                String extension = split[split.length - 1];

                Iterator readers = ImageIO.getImageReadersByFormatName(extension);
                ImageReader reader = (ImageReader) readers.next();

                reader.setInput(iis, true);

                ImageReadParam param = reader.getDefaultReadParam();

                WIDTH = reader.getWidth(0);
                HEIGHT = reader.getHeight(0);

                if (bi == null || bi.getWidth() != WIDTH || bi.getHeight() != HEIGHT) {
                    WritableRaster wr = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, WIDTH, HEIGHT, 3, null);
                    bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
                    bi.setData(wr);
                }

                param.setDestination(bi);

                bi = reader.read(0, param);

                NCOMP = bi.getColorModel().getNumComponents();

                if (iu == null || !iu.valid(WIDTH, HEIGHT, NCOMP)) {
                    iu = new ImageUtils(WIDTH, HEIGHT, NCOMP);
                }
                if (img == null || img.length != WIDTH * HEIGHT * NCOMP) {
                    img = iu.newImage();
                }

                iu.bufferedImageToArray(bi, img);
                
            } catch (IOException ex) {
                logger.error(ex);
            }
            processNewImage();
        }
    }

    @Override
    public synchronized void reset() {
        setDir(dir.toString());
    }

    @Override
    protected void postprocessImage(BufferedImage bi) {
    }
}
